package com.zlzxm.shoesmarket.util;

import android.text.TextUtils;

import java.io.File;
import java.io.IOException;

/**
 * CREATE BY zlzxm ON 11/25/20
 */
public class ZFileUtil {

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static File createFile(String path){

        File file = new File(path);

        if(file.exists()){

            return file;
        }else {

            File parent = file.getParentFile();

            if(parent != null){

                if(!parent.exists() ){

                    if( !parent.mkdirs()) {

                        return null;
                    }

                }

                try {

                    file.createNewFile();

                    return  file;
                } catch (IOException e) {
                    e.printStackTrace();

                    return  null;
                }


            }else {

                //没有这种情况

                return  null;

            }

        }

    }


    public static boolean isFilePathUseAble(String path){

        if(TextUtils.isEmpty(path)){

            return false;
        }


        return isFileExists(path);

    }


    public static boolean isFileExists(String path){

        if(TextUtils.isEmpty(path)){

            return false;
        }

        File file = new File(path);

        if(file == null){

            return false;
        }

        return  file.exists();

    }


    public   static String  getFileName(String url){

        int start = url.lastIndexOf("/")+1;

        return  url.substring(start);

    }


    public static void deleteFile(String path){

        if(isFilePathUseAble(path)){

            File file = new File(path);

            if(file.isDirectory()){

                File[] files = file.listFiles();

                for (File subFile : files) {

                    if(subFile.isDirectory()){

                        deleteFile(subFile.getPath());

                    }else {

                        subFile.delete();

                    }

                }

            }
            file.delete();


        }

        return;
    }



}
